import api from '@/common/request/index.js'
let imgUrl = 'https://mystore-1304299581.cos.ap-guangzhou.myqcloud.com/static/image/'
import tools from '@/common/utils/tools.js'
const state = {
	goods: [],
    order: [],
    info_box: {
        success_img: "/static/image/pay_success.png",
        fail_img: "/static/image/pay_failure.png",
        info: [],
        fail_reason: "取消支付"
    },
    myorder: [],
    canUser: [],
    couponsPoint: 0,    // 优惠券积分
    coupons: {
		key: ''
	},        // 优惠券
    selectAct: -1,
    orderDetail: {}, // 订单详情
    group: {},        // 拼团所需信息
    type: '',         // 类型
    orderBtn: ''  ,// 跳转订单或者团购页面
    pay: {} ,// 支付成功
	sumInit: 0, // 原始积分
	allEqual: false , // 判断购物车积分是否一致
	maxPoint: 0,
	pointList: [], // 积分列表
	credit: null, // 选择优惠券信息
	isUseFit: false ,// 是否使用优惠券
	isUnload: false, // 优惠券页面卸载
}

const mutations = {
	set_order(state, payload) {
        state.order= [...new Set(payload)]
    },
    set_group(state, payload) {
        state.order = payload;
    },
    set_type(state, payload) {
        state.type = payload;
    },
    set_orderBtn(state, payload) {
        state.orderBtn = payload;
    },
    set_myorder(state, payload) {
        state.myorder= payload
    },
    set_canUser(state, payload) {
        state.canUser= payload
    },
    set_couponsPoint(state, payload) {
        state.couponsPoint= payload
    },
    set_coupons(state, payload) {
        state.coupons= payload
    },
    set_selectAct(state, payload) {
        state.selectAct= payload
    },
    set_orderDetail(state, payload) {		
        state.orderDetail= payload
    },
    set_pay(state, payload) {
		if(typeof(payload.submit_time) == 'number' ) {
			payload.submit_time = tools.time_data(payload.submit_time, 'g_data');
		}
		state.pay= payload;
    },
}
const actions = {
	// 订单列表
    get_order({commit}, payload) {
        commit('set_order', payload)
    },
    // 我的订单
    get_myorder({commit}, payload) {
        commit('set_myorder', payload)
    },
    // 可用优惠券列表
    get_canUser({commit}, payload) {
        commit('set_canUser', payload)
    },
    // 优惠券积分数
    get_couponsPoint({commit}, payload) {
        commit('set_couponsPoint', payload)
    },
    // 优惠券
    get_coupons({commit}, payload) {
        commit('set_coupons', payload)
    },
    // 选择优惠券下标
    get_selectAct({commit}, payload) {
        commit('set_selectAct', payload)
    },
    // 订单详情
    get_orderDetail({commit}, payload) {
        commit('set_orderDetail', payload)
    },
    get_group({commit}, payload) {
        commit('set_group', payload);
    },
    get_type({commit}, payload) {
        commit('set_type', payload);
    },
    get_orderBtn({commit}, payload) {
        commit('set_orderBtn', payload);
    },
    get_pay({commit}, payload) {
        commit('set_pay', payload);
    },
    // 取消临时订单
    get_cancel({commit, state}, payload) {
        return new Promise((resolve, reject) => {
            api('orderCancel', payload).then(res => {
                if (res.status === 1) {
                    resolve(res.data);
                }
            })
        })
    },
    // 确认收货
    get_receive({commit, state}, payload) {
        return new Promise((resolve, reject) => {
            api('orderReceive', payload).then(res => {
                if (res.status === 1) {
                    resolve(res.data);
                }
            })
        })
    },
	get_sumInit({commit, state}, payload) {
		state.sumInit= payload;
	},
	get_allEqual({commit, state}, payload) {
		state.allEqual= payload;
	},
	get_maxPoint({commit, state}, payload) {
		state.maxPoint= payload;
	},
	get_pointList({commit, state}, payload) {
		state.pointList= payload;
	},
	get_paymoney({commit, state}, payload) {
		state.pay= payload;
	},
	get_credit({commit, state}, payload) {
		state.credit= payload;
	},
	get_isUseFit({commit, state}, payload) {
		state.isUseFit= payload;
	},
	get_isUnload({commit, state}, payload) {
		state.isUnload= payload;
	},
}



export default {
	namespaced: true,
	state,
	mutations,
	actions
}
